﻿using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace WebComponents.UI.Controls
{
    public class ConvertTime
    {
        #region Server Time

        public static DateTime ServerTime()
        {
            return (DateTime.Now).ToUniversalTime();
        }

        public static DateTime ServerTime(DateTime datetime)
        {
            return datetime.ToUniversalTime();
        }

        #endregion
    
        #region DateTime Converting

        public static DateTime GetDateRange(DateTime dateValue, string rangeFilter, string returnAs)
        {
            ///
            /// dateValue: processing time...
            /// rangeFilter: text description - today, yesterday, this week, previous month, this quarter
            /// returnAs: for return a value to searching textbox startDate or endDate
            ///

            string startDay = "Monday";
            DateTime dtime = DateTime.Now;
            switch (rangeFilter.ToLower())
            {
                case "tomorrow":
                    dtime = returnAs == "start" ? DateTime.Now : DateTime.Now.AddDays(1);
                    break;
                case "today":
                    // set default
                    break;
                case "yesterday":
                    dtime = returnAs == "start" ? DateTime.Now.AddDays(-1) : DateTime.Now;
                    break;
                case "this week":
                    dtime = returnAs == "start" ? GetDayOfWeek(dateValue, startDay) : DateTime.Now;
                    break;
                case "previous week":
                    dtime = returnAs == "start" ? GetDayOfWeek(dateValue.AddDays(-7), startDay) : GetDayOfWeek(dateValue, startDay).AddDays(-1);
                    break;
                case "previous month":
                    dtime = returnAs == "start" ? Convert.ToDateTime(DateTime.Now.AddMonths(-1).ToString("MM/01/yyyy")) :
                        Convert.ToDateTime(DateTime.Now.ToString("MM/01/yyyy")).AddDays(-1);
                    break;
                case "this month":
                    dtime = returnAs == "start" ? Convert.ToDateTime((DateTime.Now.ToString("MM/01/yyyy"))) : DateTime.Now;
                    break;
                case "previous quarter":
                    DateTime endOfPrevQuarter = GetThisQuater(DateTime.Now).AddDays(-75);
                    dtime = returnAs == "start" ? GetThisQuater(endOfPrevQuarter) : GetThisQuater(DateTime.Now).AddDays(-1);
                    break;
                case "this quarter":
                    dtime = returnAs == "start" ? GetThisQuater(DateTime.Now) : DateTime.Now;
                    break;
                case "previous year":
                    dtime = returnAs == "start" ? Convert.ToDateTime((DateTime.Now.AddYears(-1).ToString("01/01/yyyy"))) :
                        Convert.ToDateTime(DateTime.Now.ToString("01/01/yyyy")).AddDays(-1);
                    break;
                case "this year":
                    dtime = returnAs == "start" ? Convert.ToDateTime((DateTime.Now.ToString("01/01/yyyy"))) : DateTime.Now;
                    break;
                default:
                    // set default
                    break;
            }
            return dtime;
        }
   
        public static DateTime GetThisQuater(DateTime value)
        {
            DateTime[] quarters = new DateTime[]
            {
                new DateTime(value.Year, 1, 1),
                new DateTime(value.Year, 4, 1),
                new DateTime(value.Year, 7, 1),
                new DateTime(value.Year, 10, 1)
            };

            DateTime nearest = DateTime.MinValue;
            long lowestDiff = long.MaxValue;
            long diff;

            for (int i = 0; i < quarters.Length; i++)
            {
                diff = Math.Abs(value.Ticks - quarters[i].Ticks);
                if (diff < lowestDiff)
                {
                    lowestDiff = diff;
                    nearest = quarters[i];
                }
            }

            return nearest;
        }

        public static DateTime GetDayOfWeek(DateTime value, string day)
        {
            if (IsTheDay(value, day))
                return value;
            else
                for (int i = 1; i < 7; i++)
                {
                    if (IsTheDay(value.AddDays((-1) * i), day))
                        return value.AddDays((-1) * i);
                }
            return value;
        }

        public static bool IsTheDay(DateTime value, string day)
        {
            if (value.ToString("dddd").ToLower() == day.ToLower())
                return true;
            return false;
        }

        #endregion
    }
}
